package de.tabuma.pm.projectnavigation.controller;

import de.tabuma.pm.project.entity.Project;
import de.tabuma.pm.projectnavigation.entity.ProjectNavigation;
import de.tabuma.pm.projectnavigation.entity.ProjectNavigationWrapper;
import de.tabuma.pm.projectnavigation.logik.ProjectNavigationFacade;
import de.tabuma.lib.tabumacontroller;
import de.tabuma.pm.*;
import java.io.PrintWriter;
import java.util.*;
import java.util.logging.*;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class remove extends tabumacontroller {

    private static final Logger log = Logger.getLogger(remove.class.getName());

    public remove(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        if(request.getParameter(constants.ProjectNavigationRemove)!=null) {

            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            String navid = request.getParameter(constants.ProjectNavigationId);
            int navidInt = Integer.valueOf(navid).intValue();

            request.setAttribute(constants.ProjectNavigationId, navid);
            request.setAttribute(constants.ProjectNavigationRemove, "true");

            ProjectNavigation pn = ProjectNavigationFacade.get(navidInt);
            request.setAttribute(constants.ProjectNavigation, pn);

        }
        
        if(request.getParameter(constants.ProjectNavigationRemove1)!=null) {
            
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            String navid = request.getParameter(constants.ProjectNavigationId);
            int navidInt = Integer.valueOf(navid).intValue();

            ProjectNavigation pn = ProjectNavigationFacade.get(navidInt);
            List<ProjectNavigation> pnList = ProjectNavigationFacade.getSubnavigationList(navidInt);
            for(int i=0;i<pnList.size();i++) {
                ProjectNavigation pn1 = pnList.get(i);
                ProjectNavigationFacade.remove(pn1.getId().intValue(), user.getEmail());
            }
            ProjectNavigationFacade.remove(pn.getId().intValue(), user.getEmail());

            Project project = (Project) request.getAttribute(constants.paramProject);

            pnList = ProjectNavigationFacade.getList(project.getId().intValue());

            pn = new ProjectNavigation();
            pn.setId(new Long(0));
            pn.setProjectId(project.getId().intValue());
            pn.setNavMode(0);

            ProjectNavigationWrapper pnw = new ProjectNavigationWrapper(pn, -1);
            pnw.loadSubElement(pnList);
            request.setAttribute(constants.ProjectNavigationWrapper,pnw);
        }

    }

}
